import axios from 'axios'
import { defineStore } from 'pinia'
//定义ts接口数据类型
interface todoRaw {
    id: number,
    title: string,
    status: boolean
}
//定义容器
const useTodoStore = defineStore('todo', {
    state: () => {
        return {
            //存储数据的地方
            todoList: [] as todoRaw[],
            //用来判断
            allList: [] as todoRaw[],
            // todoDataShow: [] as todoRaw[]
        }
    },
    getters: {
        items(): number {
            return this.todoList.length
        }
    },
    actions: {
        //使用定义的ts接口
        //容器中修改或者添加state数据的方法
        addTodoStore(tododata: todoRaw[]) {
            this.todoList = tododata
            this.allList = tododata
            // this.todoDataShow = tododata
        },
        //添加到页面
        pushTodoData(tododata: todoRaw) {
            this.todoList.push(tododata)
        },
        //页面删除
        delTodoData(index: any) {
            this.todoList.splice(index, 1);
        },
        //所有任务
        allTodoData() {
            this.todoList = this.allList
        },
        //未完成的任务
        OutTodoDate() {
            this.todoList = this.allList
            this.todoList = this.allList.filter((item) => item.status == false)
        },
        //已完成的任务
        overTodoData() {
            this.todoList = this.allList
            this.todoList = this.allList.filter((item) => item.status)
        },
        //全选
        allChecked() {
            // this.todoList = this.allList
            this.todoList = this.allList.filter((item, index) => {
                if (item.status == false) {
                    const todoDataShow = {
                        id: item.id,
                        title: this.allList[index].title,
                        status: true
                    }
                    axios.put('http://localhost:8000/todo/' + item.id, todoDataShow)
                    this.todoList[index].status = true
                }else{
                    const todoDataShow = {
                        id: item.id,
                        title: this.allList[index].title,
                        status: false
                    }
                    axios.put('http://localhost:8000/todo/' + item.id, todoDataShow)
                    this.todoList[index].status = false
                }

                return []
            })
            // for (var i = 0; i < this.todoList.length; i++) {
            //     if (this.todoList[i].status == false) {
            //         const todoDataShow = {
            //             id: this.todoList[i].id,
            //             title: this.todoList[i].title,
            //             status: true
            //         }
            //         axios.put('http://localhost:8000/todo/' + this.todoList[i].id, todoDataShow)
            //     }

            // }
           
        }
    }
})
export { useTodoStore, todoRaw }


